<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class nominasi extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		//$this->output->enable_profiler(true);
		$this->load->model('nominasi_model');
	}
	
	function index(){
		$this->load->view('nominasikks_view');
	}
	
	function grid(){
		$data = $this->nominasi_model->getdata();
		echo $this->nominasi_model->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['ID'] = '';
		$data['ID_KPS'] = '';
		$data['TAHUN'] = date('Y');
		$data['BULAN'] = (int) date('m');
		$data['TANGGAL'] = date('d/m/Y');
		$data['NOMINASI'] = '';
		
		$this->load->view('nominasi_form_view',$data);
	}
	
	function edit($kode){
		$r = $this->nominasi_model->getdataedit($kode);
		
		$data['ID'] = $r->row()->ID;
		$data['ID_KPS'] = $r->row()->ID_KPS;
		$data['TAHUN'] = $r->row()->TAHUN;
		$data['BULAN'] = $r->row()->BULAN;
		$data['TANGGAL'] = FormatDateFromMysql($r->row()->TANGGAL);
		$data['NOMINASI'] = $r->row()->NOMINASI;
		$data['aksi'] = 'edit';
		$this->load->view('nominasi_form_view',$data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		
			if($aksi=="add"){ // add
				
				# rules validasi form
			$this->form_validation->set_rules("ID_KPS", 'Nama KPS', 'trim|required|xss_clean');
		
				# message rules
			$this->form_validation->set_message('required', 'Field %s harus diisi.');

			$data['pesan_error'] = '';
			if ($this->form_validation->run() == FALSE){
				$data["pesan_error"] .= trim(form_error('ID_KPS',' ',' '))==''?'':form_error('ID_KPS',' ',' ').'<br>';
				//$data["pesan_error"] .= trim(form_error('ID_KPS',' ',' '))==''?'':form_error('ID_KPS',' ',' ').'<br>';
			}else{
				// cek di tabel sendiri -> T_NOMINASI_KKKS
				if(!$this->nominasi_model->EXIST_T_NOMINASI_KKKS($data)){
					// cek data di tabel lq_legal_task
					if(!$this->nominasi_model->EXIST_LQ_LEGAL_TASK($data)){
						$result = $this->nominasi_model->InsertOnDb($data);
						if(!$result){
							$data['pesan_error'] .= 'Gagal Insert!';
						}
					}else{
						$data['pesan_error'] .= 'Sudah terdapat data dalam database!';
					}
				}else{
					$data['pesan_error'] .= 'Sudah terdapat data dalam database!';
				}
				}
			}else { // edit
				
				// cek di tabel sendiri -> T_NOMINASI_KKKS
				if(!$this->nominasi_model->EXIST_EDIT_T_NOMINASI_KKKS($data)){
					if(!$this->nominasi_model->EXIST_LQ_LEGAL_TASK($data)){
						$result = $this->nominasi_model->UpdateOnDb($data);
						if(!$result){
							$data['pesan_error'] .= 'Gagal Insert!';
						}
					}else{
						$data['pesan_error'] .= 'Sudah terdapat data dalam database!';
					}
					
				}else{
					$data['pesan_error'] .= 'Sudah terdapat data dalam database!';
				}
				
			}
		
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode,$ID_KPS,$TAHUN,$BULAN){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		$data['ID'] = $kode;
		$data['ID_KPS'] = $ID_KPS;
        $data['TAHUN'] = $TAHUN;
        $data['BULAN'] = $BULAN;


		// cek data di tabel realiasi revenue -> T_REALISASI_REVENUE
		if(!$this->nominasi_model->EXIST_LQ_LEGAL_TASK($data)){
		
			$result = $this->nominasi_model->DeleteOnDb($kode);
			if(!$result){
				$data['pesan_error'] .= 'Gagal delete!';
			}
		}else{
			$data['pesan_error'] .= 'Data Sudah di Realisasi';
		}
				
		//$result = $this->nominasi_model->DeleteOnDb($kode);
		
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal sudah ada di realiasi'));
		}
	}
	
	
	
}